package com.example.cardgame
import android.content.Intent
import android.content.pm.ActivityInfo
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.Target



@Suppress("DEPRECATION")
class test_main : AppCompatActivity() {

    // 声明 ActivityResultLauncher
    private lateinit var editResultLauncher: ActivityResultLauncher<Intent>
    private var currentAvatarUri: Uri? = null  // 添加头像 URI
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.test_first)

        // 强制竖屏
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        val username:TextView=findViewById(R.id.username)
        val sno:TextView=findViewById(R.id.sno)
        val telephone:TextView=findViewById(R.id.telephone)
        val email:TextView=findViewById(R.id.email)
        val gender = findViewById<TextView>(R.id.gender)
        val hobbies = findViewById<TextView>(R.id.hobby)
        val highScore = findViewById<TextView>(R.id.highScore)
        if(savedInstanceState!=null){
            username.text=savedInstanceState.getString("username")
            sno.text=savedInstanceState.getString("sno")
            telephone.text=savedInstanceState.getString("telephone")
            email.text=savedInstanceState.getString("email")
            gender.text=savedInstanceState.getString("gender")
            hobbies.text=savedInstanceState.getString("hobbies")
            currentAvatarUri=savedInstanceState.getParcelable("avatarUri")
            highScore.text=savedInstanceState.getString("highScore")
        }



        // 初始化 ActivityResultLauncher，用于处理返回的数据
        editResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            //修改个人信息
            if (result.resultCode == 6) {
                val data = result.data
                data?.let {
                    val userInfo = it.getStringArrayExtra("userInfo")
                    userInfo?.let { info ->
                        if (!info[0].isNullOrEmpty()) {
                            username.text = info[0]
                        }
                        if (!info[1].isNullOrEmpty()) {
                            sno.text = info[1]
                        }
                        if (!info[2].isNullOrEmpty()) {
                            telephone.text = info[2]
                        }
                        if (!info[3].isNullOrEmpty()) {
                            email.text = info[3]
                        }
                        if (!info[4].isNullOrEmpty()) {
                            gender.text = info[4]
                        }
                        if (info.drop(6).any { it.isNotEmpty() }) {
                            hobbies.text = info.drop(6).joinToString(", ")
                        }

                        // 处理图片 URI
                        val imageUriString = info[5]  // 获取图片 URI
                        if (imageUriString.isNotEmpty()) {
                            val imageUri = Uri.parse(imageUriString)
                            displayImage(imageUri)  // 确保这个方法能正确处理 URI
                            currentAvatarUri = imageUri // 更新当前页面的头像
                        } else if (currentAvatarUri != null) {
                            // 如果传回的 URI 为空，但当前页面已有头像，则保留当前头像
                            displayImage(currentAvatarUri!!)
                        }
                    }
                }
            }

            //处理游戏最高分
            else if (result.resultCode == 2) {
                val tempScore = result.data?.getIntExtra(FIRST_MSG, 0) // 默认值为 0
                val currentHighScore = highScore.text.toString().toIntOrNull() ?: 0
                if (tempScore != null && tempScore > currentHighScore) {
                    highScore.text = tempScore.toString() // 更新最高分数
                }
            }
        }


        //修改个人信息`
        val edit: Button = findViewById(R.id.edit)
        edit.setOnClickListener {
            val intent = Intent(this, test_second::class.java)
            intent.putExtra("username", username.text.toString())
            intent.putExtra("sno", sno.text.toString())
            intent.putExtra("phoneNumber", telephone.text.toString())
            intent.putExtra("email", email.text.toString())
            intent.putExtra("currentUri", currentAvatarUri?.toString()) // 传递当前头像 URI
            editResultLauncher.launch(intent)
        }


        //翻牌游戏
        val game: Button = findViewById(R.id.game)
        game.setOnClickListener {
            val intent = Intent(this@test_main, MainActivity::class.java)
            editResultLauncher.launch(intent)
        }
    }
    private fun displayImage(imageUri: Uri) {
        val imageView = findViewById<ImageView>(R.id.imageView)
        Glide.with(this)
            .load(imageUri)
            .override(Target.SIZE_ORIGINAL) // 确保加载原始尺寸的图片
            .into(imageView)
    }
    //保持现有状态
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putString("username", findViewById<TextView>(R.id.username).text.toString())
        outState.putString("sno", findViewById<TextView>(R.id.sno).text.toString())
        outState.putString("telephone", findViewById<TextView>(R.id.telephone).text.toString())
        outState.putString("email", findViewById<TextView>(R.id.email).text.toString())
        outState.putString("gender", findViewById<TextView>(R.id.gender).text.toString())
        outState.putString("hobbies", findViewById<TextView>(R.id.hobby).text.toString())
        outState.putParcelable("avatarUri", currentAvatarUri)  // 保存头像 URI
        outState.putString("highScore",findViewById<TextView>(R.id.highScore).text.toString())
    }
    override fun onResume() {
        super.onResume()
        // 在恢复时强制竖屏
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    }

}


